﻿using System.Collections.Generic;
using System.Linq;

namespace KadalaSpree.XCore
{
    public static class XElements
    {
        #region Fields

        private static readonly Dictionary<ulong, XElement> ElementsByHash = new Dictionary<ulong, XElement>();

        #endregion

        #region Common Elements

            public static XElement KadalaWindow
            {
                get { return GetElementByName(ElementName.MysteryVendorWindow); }
            }

        #endregion

        #region Methods

        public static XElement GetElementByHash(ulong hash)
        {
            if (!ElementsByHash.ContainsKey(hash))
            {
                var element = new XElement(hash);
                ElementsByHash.Add(hash, element);
            }
            else
            {
                if (!ElementsByHash[hash].IsValid)
                {
                    ElementsByHash[hash].Update();
                }
            }
            return ElementsByHash[hash];
        }

        public static XElement GetElementByName(ElementName name)
        {
            var hash = XLookup.Elements.ElementNameAndHash.FirstOrDefault(e => e.Key == name).Value;
            return GetElementByHash(hash);
        }

        public static List<XElement> GetJewelerGemElements(ElementName type)
        {
            return XLookup.Elements.Vendors.JewelerElementNameAndHash.Select(gem => GetElementByHash(gem.Value)).ToList();
        }

        public static XElement GetJewelerElementByGemType(ElementName type)
        {
            return GetElementByHash(XLookup.Elements.Vendors.JewelerElementNameAndHash[type]);
        }

        #endregion
    }
}